﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TeamBuy.AccessService
{
    public class TeamBuyCardService : Common.ITeamBuyCard
    {
        public System.Data.DataTable GetCardByID(int cardID)
        {
            return TeamBuy.AccessService.AccessHelper.GetDataSet("select * from S_Card where CardID=" + cardID);

        }

        public System.Data.DataTable GetCardByCardNumber(string cardNumber)
        {
            return TeamBuy.AccessService.AccessHelper.GetDataSet("select * from S_Card where CardNumber='" + cardNumber + "'");
        }

        public bool GetCardUsed(string cardNumber)
        {
            bool getIsUsed = Convert.ToBoolean(AccessHelper.GetDataSet("select IsUsed from S_Card where CardNumber='" + cardNumber + "'").Rows[0]["IsUsed"]);
            return getIsUsed;
        }

        public int SetCardToUsedByCardNumber(string cardNumber, int userID)
        {
            bool getIsUsed = Convert.ToBoolean(AccessHelper.GetDataSet("select IsUsed from S_Card where CardNumber='" + cardNumber + "'").Rows[0]["IsUsed"]);
            if (getIsUsed == false)
            {
                int updateToUsed;
                updateToUsed = TeamBuy.AccessService.AccessHelper.ExecuteCommand("update S_Card set IsUsed=1 where CardNumber='" + cardNumber + "' and UserID=" + userID);
                if (updateToUsed == 1)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return -1;
            }
        }


        public int SetCardToPaiedByCardNumber(string cardNumber, int userID)
        {
            bool getIsPaied = Convert.ToBoolean(AccessHelper.GetDataSet("select IsPaied from S_Card where CardNumber='" + cardNumber + "'").Rows[0]["IsPaied"]);
            if (getIsPaied == false)
            {
                int updateToPaied;
                updateToPaied = TeamBuy.AccessService.AccessHelper.ExecuteCommand("update S_Card set IsPaied=1, UserID=" + userID + " where CardNumber='" + cardNumber + "'");
                if (updateToPaied == 1)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return -1;
            }
        }
    }
}
